# import requests
# from util.headers import get_header
# url = 'https://tieba.baidu.com/t/f/?class=college'
# cookie ={
#     'ariaappid':'c890648bf4dd00d05eb9751dd0548c30',
#     'ariauseGraymode':'false',
#     'BAIDU_WISE_UID':'wapp_1761653859203_290',
#     'BDUSS_BFESS':'tiY25wMXlRdn44czJYMEtDUVNRb2lYenVwRG1NRlU0dVdwRUk1SjZJYm5iS0ZvRVFBQUFBJCQAAAAAAQAAAAEAAAAEmOhIYmxpbmRvdmVyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOffeWjn33loLU	',
#     'BDUSS':'tiY25wMXlRdn44czJYMEtDUVNRb2lYenVwRG1NRlU0dVdwRUk1SjZJYm5iS0ZvRVFBQUFBJCQAAAAAAQAAAAEAAAAEmOhIYmxpbmRvdmVyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOffeWjn33loLU',
#
# }
# res = requests.get(url,headers=get_header())
# print(res.text)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
一键拉取「高校贴吧」列表
tieba.baidu.com/t/f/?class=college
"""

# import re
# import time
# import requests
# from requests.adapters import HTTPAdapter
# from urllib3 import Retry
#
# # 1. 把你给的 Cookie 直接粘进来（已清理多余空格，\n 可保留）
# COOKIE = (
#     'BAIDUID_BFESS=FA3B8838A0D1C5C02A48E89FA0FE811B:FG=1; '
#     'BAIDUID=FA3B8838A0D1C5C02A48E89FA0FE811B:FG=1; '
#     'BDUSS=tiY25wMXlRdn44czJYMEtDUVNRb2lYenVwRG1NRlU0dVdwRUk1SjZJYm5iS0ZvRVFBQUFBJCQAAAAAAQAAAAEAAAAEmOhIYmxpbmRvdmVyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOffeWjn33loLU; '
#     'BDUSS_BFESS=tiY25wMXlRdn44czJYMEtDUVNRb2lYenVwRG1NRlU0dVdwRUk1SjZJYm5iS0ZvRVFBQUFBJCQAAAAAAQAAAAEAAAAEmOhIYmxpbmRvdmVyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOffeWjn33loLU; '
#     'STOKEN=13ba977d13785c18909be88ebd83c9b3963c83bcf500322201619da2a919a3a1; '
#     'STOKEN_BFESS=99b0ba9f46b970206bcc95850bb0faf75ce750ab519a63b63f5d211935262e00; '
#     'H_WISE_SIDS=63140_64006_64976_65245_65621_65634_65664_65730_65759_65782_65794_65823_65894_65853_65920_65361_65928_65942_65953_65955_65989_65999; '
#     'H_WISE_SIDS_BFESS=61671_62325_63140_63584_63639_63672_63683_63724_63713_63274_63775_63806_63881_63915_63885; '
#     'XFCS=2968A7718C851ACDFD7E7449380DEE0C2111AA0B4E9CB502DDCD3002DF674556; '
#     'XFI=099df1e0-b3f9-11f0-a225-67aeed1ecbd3; '
#     'XFS=69EGdjKo4%2BBY1e6EKA7FqGmIUWKhhWFGmKlwRhrKJtg%3D'
# )
#
# HEADERS = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
#     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
#     'Accept-Language': 'zh-CN,zh;q=0.9',
#     'Cookie': COOKIE,
#     'Referer': 'https://tieba.baidu.com/'
# }
#
# # 2. 接口地址
# URL = 'https://tieba.baidu.com/t/f'
# PARAMS = {'class': 'college'}
#
# # 3. 正则提取学校名（tieba 高校页固定格式）
# PATTERN = re.compile(r'<a[^>]*?href="/f\?kw=[^"]*"[^>]*?>([^<]+)</a>')
#
# session = requests.Session()
# retry = Retry(total=3, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
# adapter = HTTPAdapter(max_retries=retry, pool_connections=20, pool_maxsize=50)
# session.mount('https://', adapter)
# session.mount('http://', adapter)
# TIMEOUT = 30   # 秒
#
# def fetch_schools() -> set:
#     schools, page = set(), 1
#     while True:
#         try:
#             resp = session.get(URL,
#                                params={**PARAMS, 'pn': page},
#                                headers=HEADERS,
#                                timeout=TIMEOUT)
#             resp.raise_for_status()
#         except requests.ReadTimeout:
#             print(f'[!] 第 {page} 页超时，30 s 没拉完，重试 …')
#             continue
#         except requests.RequestException as e:
#             print(f'[!] 网络异常：{e}，跳过该页')
#             page += 1
#             time.sleep(2)
#             continue
#
#         items = PATTERN.findall(resp.text)
#         if not items:
#             break
#         schools.update(items)
#         print(f'[+] 第 {page} 页抓到 {len(items)} 个，累计 {len(schools)} 个')
#         page += 1
#         time.sleep(0.8)   # 比上次再慢一点
#     return schools
#
# if __name__ == '__main__':
#     print('开始拉取，超时已放宽到 30 s，最多重试 3 次 …')
#     schools = fetch_schools()
#     print('\n===== 高校贴吧名单（共 {} 所） ====='.format(len(schools)))
#     for name in sorted(schools):
#         print(name)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
爬取 tieba.baidu.com/t/f/?class=college 高校列表
解决「握手成功但服务器一直不返回数据」的问题
"""
import re
import time
from curl_cffi import requests as brws

# # 1. 你的 Cookie（原样粘贴）
# COOKIE = (
#     'BAIDUID_BFESS=FA3B8838A0D1C5C02A48E89FA0FE811B:FG=1; '
#     'BAIDUID=FA3B8838A0D1C5C02A48E89FA0FE811B:FG=1; '
#     'BDUSS=tiY25wMXlRdn44czJYMEtDUVNRb2lYenVwRG1NRlU0dVdwRUk1SjZJYm5iS0ZvRVFBQUFBJCQAAAAAAQAAAAEAAAAEmOhIYmxpbmRvdmVyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOffeWjn33loLU; '
#     'BDUSS_BFESS=tiY25wMXlRdn44czJYMEtDUVNRb2lYenVwRG1NRlU0dVdwRUk1SjZJYm5iS0ZvRVFBQUFBJCQAAAAAAQAAAAEAAAAEmOhIYmxpbmRvdmVyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOffeWjn33loLU; '
#     'STOKEN=13ba977d13785c18909be88ebd83c9b3963c83bcf500322201619da2a919a3a1; '
#     'STOKEN_BFESS=99b0ba9f46b970206bcc95850bb0faf75ce750ab519a63b63f5d211935262e00; '
#     'H_WISE_SIDS=63140_64006_64976_65245_65621_65634_65664_65730_65759_65782_65794_65823_65894_65853_65920_65361_65928_65942_65953_65955_65989_65999; '
#     'H_WISE_SIDS_BFESS=61671_62325_63140_63584_63639_63672_63683_63724_63713_63274_63775_63806_63881_63915_63885; '
#     'XFCS=2968A7718C851ACDFD7E7449380DEE0C2111AA0B4E9CB502DDCD3002DF674556; '
#     'XFI=099df1e0-b3f9-11f0-a225-67aeed1ecbd3; '
#     'XFS=69EGdjKo4%2BBY1e6EKA7FqGmIUWKhhWFGmKlwRhrKJtg%3D'
# )
#
# HEADERS = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
#                   '(KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
#     # 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#     # 'Accept-Language': 'zh-CN,zh;q=0.9',
#     # 'Accept-Encoding': 'gzip, deflate, br',
#     'Referer': 'https://tieba.baidu.com/',
#     # 'Cookie': COOKIE
# }
#
# URL = 'https://tieba.baidu.com/t/f'
# # PATTERN = re.compile(r'<a[^>]*?href="/f\?kw=[^"]*"[^>]*?>([^<]+)</a>')
#
# def main():
#     page, schools = 1, set()
#     resp = brws.get(URL,
#                     # params={'class': 'college', 'pn': page},
#                     # headers=HEADERS,
#                     # impersonate="chrome120",   # 关键：浏览器指纹
#                     )
#     resp.raise_for_status()
#     print(resp.status_code)
#
# if __name__ == '__main__':
#     main()
# curl_cffi = “给 Python 穿上浏览器外衣”：同样的代码，只需加 impersonate="chrome120"，就能把 TLS 指纹、HTTP/2 设置全部换成浏览器同款，
# 静默突破基于指纹的反爬与封禁，而无需启动真正的浏览器或承受 Selenium 的资源开销。

from curl_cffi import requests
import re
url ='https://tieba.baidu.com/t/f/?class=college'
res = requests.get(url)
# print(res.text)
data_id = re.findall(r'data-fid="(.*?)">(.*?)</a>',res.text)
print(data_id)
# print(data_id[0][1])
for i in range(len(data_id)):
    url_data=f'https://tieba.baidu.com/t/f/{data_id[i][0]}'
    res1=requests.get(url_data)
    print(res1.url)
    print(res1.text)
    break